#!/bin/bash -e

if [ $# -lt 1 ]; then
   echo "usage: $0 <repo name>"
   exit 1
fi

REPO=$1
shift

cache_dir=$(mktemp -d)
download_dir=$(mktemp -d)

rname=$(sed -nr '/^\[(.*)\]$/s@^\[(.*)\]$@\1@p' ${REPO})

dl=$(echo $@ | grep downloadonly)

if [ -z "$dl" ]; then
    yum --setopt cachedir=${cache_dir} --disablerepo=* --enablerepo=${rname} -c ${REPO} $@
else
    echo "download package to ${download_dir}"
    yumdownloader --setopt cachedir=${cache_dir} --destdir ${download_dir} --disablerepo=* --enablerepo=${rname} -c ${REPO} $@
    set -x
    ls -l ${download_dir}
    set +x
    echo "download pcakge is ok!"
fi

rm -rf ${cache_dir}
rm -rf ${download_dir}

